package ph.com.gs3.chat.fragments.view;

import ph.com.gs3.chat.R;
import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class TestViewFragment extends Fragment {

	public static final String TAG = TestViewFragment.class.getSimpleName();

	private TextView tvIncomingMessage;
	private EditText etOutgoingMessage;
	private Button bSend;

	private TestViewActionListener listener;

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);

		try {
			listener = (TestViewActionListener) activity;
		} catch (ClassCastException ex) {
			throw new IllegalStateException(activity.getClass().getSimpleName()
			        + " must implement " + TestViewActionListener.class.getSimpleName());
		}

	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

		View rootView = inflater.inflate(R.layout.fragment_test, container, false);

		tvIncomingMessage = (TextView) rootView.findViewById(R.id.Test_tvIncomingMessage);
		etOutgoingMessage = (EditText) rootView.findViewById(R.id.Test_etOutgoingMessage);

		bSend = (Button) rootView.findViewById(R.id.Test_bSendMessage);
		bSend.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				String message = etOutgoingMessage.getText().toString();
				listener.onSendMessageCommand(message);

			}
		});

		return rootView;
	}

	public void appendIncomingMessage(String incomingMessage) {
		tvIncomingMessage.append("\n" + incomingMessage);
	}

	public static interface TestViewActionListener {
		public void onSendMessageCommand(String message);
	}

}
